import { Component, OnInit, QueryList, ViewChild, ViewChildren } from '@angular/core';
import { ApiService } from '@core/services/http/api.service';
import { ExportExcelService } from '@core/services/excel/export-excel.service';
import { ServiceRateStore } from './service-rate-store';
import { ServiceRatePersonal } from './serivce-rate-personal';
import {ApesMessageService} from "@shared/apes/message";
import {UserAuthService} from "@core/services/user-auth.service";
import { BiService } from '../../../bi.service';

@Component({
  selector: 'service-rate',
  templateUrl: './service-rate.component.html',
  styleUrls: ['./service-rate.component.scss'],
})
export class ServiceRateComponent implements OnInit {
  @ViewChild(ServiceRateStore,{static:true}) childStore: ServiceRateStore;
  @ViewChild(ServiceRatePersonal,{static:true}) childPersonal: ServiceRatePersonal;
  /* 按钮相关 */
  buttonBar = [{ hide: true, style: 'default', label: '查询', id: 'action_query',loading: false },
    { hide: true, style: 'default', label: '导出', id: 'action_export' },
    { hide: true, style: 'default', label: '重置', id: 'action_reset' },
  ];
  title = '';
  ctx = { model: {} };

  // 获取门店
  nxmdList: any[];
  arrNXMD = [];
  saleList: any [];
  sale = [];

  hind: any = 'none';
  hidn: any = 'none';
  hindd: any = 'none';
  store: any = 'block';
  personal: any = 'none';
  qfbj: number = 1;

  // 查询条件
  ngNXMD: any;
  moneyValue: number = 3000;
  timeValue: number = 2;
  mdjcTime: any = [];
  startValue: Date = null;
  endValue: Date = null;
  endDate = null;      // 默认结束时间
  startDate = null;    // 默认开始时间
  jcrName: string;
  saleID:any[];
  userInfo = this.userAuthService.getUserInfoObj();
  companyId = this.userInfo.userAccount.post.company.id;
  companyData :any;
  companyList: any[];

  constructor(private apiService: ApiService,
              private exportExcelService: ExportExcelService,
              private msg: ApesMessageService,
              private userAuthService: UserAuthService,
              private bi: BiService) {
    this.companyList = [];
    this.companyList.push(this.companyId);
  }

  ngOnInit() {
    // 默认 请求 当天 数据
    this.endDate = new Date();
    this.startDate = new Date(this.endDate.getTime() - (0 * 24 * 60 * 60 * 1000));
    this.mdjcTime.push(this.startDate);
    this.mdjcTime.push(this.endDate);
    this.bi.getCompany(this.userInfo.userAccount.post.localPermit.id).then(value => { this.companyData = value });
    this.queryData();
    // this.getNxmdData();
    // this.getSaleData();
  }

  onEvent(data: any) {

    if (data instanceof MouseEvent) return;
    if (data) {
      if (data.id === 'action_query') {
        this.buttonBar[0].loading = true;
        this.queryData();
      } else if (data.id === 'action_reset') {
        this.action_reset();
      } else if (data.id === 'action_export') {
        this.action_export();
      }
    }
  }

  // 导出数据
  action_export() {
    if(this.store === 'block'){
      let rowData = this.childStore.rowData;
      if (rowData && rowData.length === 0) {
        return;
      }
      const columnList = this.childStore.columnDefs;
      this.exportExcelService.exportWithDealData(columnList, rowData, '服务单成交率-门店');
    }else {
      let rowData = this.childPersonal.rowData;
      if (rowData && rowData.length === 0) {
        return;
      }
      const columnList = this.childPersonal.columnDefs;
      this.exportExcelService.exportWithDealData(columnList, rowData, '服务单成交率-个人');
    }

  }

  // 重置
  action_reset(): void {
    this.ngNXMD = '';
    this.moneyValue = 3000;
    this.timeValue = 2;
    this.jcrName = '';
    this.mdjcTime = '';
    this.saleID = [];
  }

  queryData() {
    // if(this.companyList.length <= 0){
    //   this.companyList.push(this.companyId);
    // }
    this.getDate(this.mdjcTime, 'startValue', 'endValue');
    if (this.mdjcTime.length === 0) {
      this.hind = 'block';
    } else if (this.moneyValue <= 0) {
      this.hind = 'none';
      this.hidn = 'block';
    } else if (this.timeValue < 0) {
      this.hind = 'none';
      this.hidn = 'none';
      this.hindd = 'block';
    } else {
      this.hind = 'none';
      this.hidn = 'none';
      this.hindd = 'none';
      if(this.store === 'block'){
        this.childStore.getStore(this.startValue, this.endValue,
          this.timeValue, this.ngNXMD,
          this.jcrName, this.qfbj, this.moneyValue, this.saleID, this.companyList);
        this.buttonBar[0].loading = false;
        this.getNxmdData();
        this.getSaleData();
      }else {
        this.childPersonal.getStore(this.startValue, this.endValue,
          this.timeValue, this.ngNXMD,
          this.jcrName, this.qfbj, this.moneyValue, this.saleID, this.companyList);
        this.buttonBar[0].loading = false;
        this.getNxmdData();
        this.getSaleData();
      }
    }
  }

  getDate(time: any[], time_ks: string, time_js: string) {
    // debugger;
    let [first, second] = time;
    if (first) {
      this[time_ks] = first.toISOString().slice(0, 10);
    } else {
      this[time_ks] = null;
    }

    if (second) {
      this[time_js] = second.toISOString().slice(0, 10);
    } else {
      this[time_js] = null;
    }

  }

  // 点击门店事件
  getStoreCheck() {
    this.jcrName = '';
    this.store = 'block';
    this.personal = 'none';
    this.qfbj = 1;
    if(this.mdjcTime !== '' && this.moneyValue > 0){
      this.childStore.getStore(this.startValue, this.endValue,
        this.timeValue, this.ngNXMD,
        this.jcrName, this.qfbj, this.moneyValue, this.saleID, this.companyList);
    }
  }

  // 点击个人事件
  getPersonalCheck() {
    this.ngNXMD = '';
    this.personal = 'block';
    this.store = 'none';
    this.qfbj = 0;
    if(this.mdjcTime !== '' && this.moneyValue > 0){
      this.childPersonal.getStore(this.startValue, this.endValue,
        this.timeValue, this.ngNXMD,
        this.jcrName, this.qfbj, this.moneyValue, this.saleID, this.companyList);
    }
  }

  // 获取门店名称
  getNxmdData() {
    let data = {
      'interfaceMethod': 'ipmBPforAppService.getSQMDXX', 'pdata': {'companyId': this.companyList},
    };
    this.apiService.post('report.interface', { 'body': JSON.stringify(data) }).subscribe(
      (resultData) => {
        // debugger;
        const result = JSON.parse(resultData);
        if (result.success) {
          if (result.obj.length !== 0) {
            let data = JSON.parse(result.obj);
            this.nxmdList = data;
            this.arrNXMD = this.nxmdList;
          }
        } else {
          this.msg.info(result.msg);
        }
      });
  }

  // 获取销售组
  getSaleData() {
    let data = {
      'interfaceMethod': 'ipmQueryXszService.queryXszData', 'pdata': { 'MJBJ': 0,'companyId': this.companyList },
    };
    this.apiService.post('report.interface', { 'body': JSON.stringify(data) }).subscribe(
      (resultData) => {
        // debugger;
        const result = JSON.parse(resultData);
        if (result.success) {
          if (result.obj.length !== 0) {
            let data = JSON.parse(result.obj);
            this.saleList = data;
            this.sale = this.saleList;
          }
        } else {
          this.msg.info(result.msg);
        }
      });
  }

}
